package com.project.easy.common.framework.web.exception.handler;

import  com.project.easy.common.util.dto.R;
import  com.project.easy.common.util.exception.BaseException;
import  com.project.easy.common.util.exception.print.PrintException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/10/13 22:22
 * @description: 全局异常拦截处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 异常处理
     * @param exception 异常
     * @return 返回异常信息
     */
    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public R exception(BaseException exception) {
        PrintException.print(exception);
        return  R.fail(exception.getSubCode(), exception.getSubMsg());
    }

    /**
     * 运行异常处理 直接抛出异常
     * @param exception 异常
     * @return 返回异常信息
     */
    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public R exception(Throwable exception) {
        PrintException.print(exception);
        R fail = R.fail("599", "系统未知异常");
        fail.setSubMsg(exception.getMessage());
        return  fail;
    }
}
